import pandas as pd
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
import joblib
import os

def load_data(test_data_path):
    data = pd.read_csv(test_data_path)
    return data

def evaluate_model(model, X_test, y_test):
    y_pred = model.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    precision = precision_score(y_test, y_pred, average='weighted')
    recall = recall_score(y_test, y_pred, average='weighted')
    f1 = f1_score(y_test, y_pred, average='weighted')
    
    return accuracy, precision, recall, f1

def main():
    # 路径已修正为和train.py一致
    model_path = r'd:\毕设\asd-classification-project\model\trained_model.pkl'
    test_data_path = r'd:\毕设\asd-classification-project\model\test_data.csv'
    model = joblib.load(model_path)
    test_data = load_data(test_data_path)

    # 最后一列是label
    X_test = test_data.iloc[:, :-1]
    y_test = test_data.iloc[:, -1]

    # Evaluate the model
    accuracy, precision, recall, f1 = evaluate_model(model, X_test, y_test)

    # Print the evaluation metrics
    print(f'Accuracy: {accuracy:.2f}')
    print(f'Precision: {precision:.2f}')
    print(f'Recall: {recall:.2f}')
    print(f'F1 Score: {f1:.2f}')

if __name__ == '__main__':
    main()